package com.copyctrl.campus.portal.controller;

import cn.hutool.json.JSONUtil;
import com.copyctrl.campus.common.model.ApiResponse;
import com.copyctrl.campus.portal.service.FoundService;
import com.copyctrl.model.CampFound;
import com.copyctrl.model.CampFoundExample;
import com.copyctrl.model.CampGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author:JH
 * @version:1.0
 */
@RestController
@RequestMapping("/found")
public class FoundController {
    @Autowired
    FoundService service;

    @PostMapping("/{found}")
    public ApiResponse add(@PathVariable String found) {
        CampFound campFound = JSONUtil.toBean(found, CampFound.class);
        int i=service.add(campFound);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("发布失败请联系管理员");
    }
    @DeleteMapping("/{id}")
    public ApiResponse delete(@PathVariable Integer id) {
        int i=service.delete(id);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("发布失败请联系管理员");
    }
    @PutMapping("/{found}")
    public ApiResponse update(@PathVariable String found) {
        CampFound campFound = JSONUtil.toBean(found, CampFound.class);
        int i=service.update(campFound);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("发布失败请联系管理员");
    }

    @GetMapping("/all/{id}")
    public ApiResponse findAll(@PathVariable int id) {
        return ApiResponse.ofSuccess(service.findAll(id));
    }
    @GetMapping("/not")
    public ApiResponse findAllNotFound() {
        return ApiResponse.ofSuccess(service.findNotFound());
    }
    @GetMapping("/{uid}")
    public ApiResponse find(@PathVariable Integer uid) {
        return ApiResponse.ofSuccess(service.findByUid(uid));
    }
}
